

{include file="public/head" /}

<body>

<div class="layui-fluid">

    <div class="layui-row layui-col-space10">
        <div class="layui-col-md12">

            <div class="layui-card">
                <div class="layui-card-header" >
                    <div class="layui-btn-container " style="line-height: 72px;">
                        <a id="export" class="layui-btn layui-btn-sm layui-btn-normal" href="javascript:;" autocomplete="off">立即备份</a>
                        <a id="optimize" href="{:url('admin/databackup/optimize')}" class="layui-btn layui-btn-sm layui-btn-warm">优化数据表</a>
                        <a id="repair" href="{:url('admin/databackup/repair')}" class="layui-btn layui-btn-sm layui-btn-danger">修复数据表</a>
                        <a  href="{:url('admin/databackup/importlist')}" class="layui-btn layui-btn-sm layui-btn-primary">还原数据库</a>
                    </div>
                </div>
                <div class="layui-card-body">

                    <div class="yys-fluid yys-wrapper">
                        <div class="layui-row lay-col-space20">
                            <div class="layui-cos-xs12 layui-col-sm12 layui-col-md12 layui-col-lg12">

                                <form id="export-form" method="post" action="{:url('admin/databackup/export')}">
                                    <table class="layui-table" lay-even="" lay-skin="row" lay-size="">

                                        <colgroup>
                                            <col width="50">
                                            <col width="150">
                                            <col width="150">
                                            <col width="150">
                                            <col width="150">
                                            <col width="200">
                                            <col width="150">
                                            <col width="200">
                                            <col width="100">
                                            <col width="200">
                                        </colgroup>

                                        <thead>
                                        <tr>
                                            <th width="48"><input lay-skin="primary" checked="chedked" type="checkbox" value=""></th>
                                            <th>表名</th>
                                            <th>数据量</th>
                                            <th>数据大小</th>
                                            <th>数据引擎</th>
                                            <th>排序规则</th>
                                            <th>表注释</th>
                                            <th>创建时间</th>
                                            <th>备份状态</th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>

                                        <tbody>
                                        {volist name='list' id='table'}
                                        <tr>
                                            <td>
                                                <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                                            </td>
                                            <td>{$table.name}</td>
                                            <td>{$table.rows}</td>
                                            <td>{$table.data_length|format_bytes}</td>
                                            <td>{$table.engine}</td>
                                            <td>{$table.collation}</td>
                                            <td>{$table.comment}</td>
                                            <td>{$table.create_time}</td>
                                            <td class="info">备份未开始</td>
                                            <td>
                                                <a  href="javascript:;" class="layui-btn layui-btn-xs optimiz" tables="{$table.name}">优化表</a>&nbsp;
                                                <a  href="javascript:;" class="layui-btn layui-btn-xs repair" tables="{$table.name}">修复表</a>
                                                <a  href="javascript:;" class="layui-btn layui-btn-xs layui-btn-normal show" data-table="{$table.name}">查看表</a>
                                            </td>
                                        </tr>
                                        {/volist}
                                        </tbody>


                                    </table>
                                </form>

                            </div>
                        </div>
                    </div>

                </div>
            </div>

        </div>
      </div>
    </div>


<script src="{__FRAME_PATH}js/jquery.min.js"></script>
<script src="{__FRAME_PATH}js/function.js"></script>

<script>
    layui.use(['jquery','layer'],function(){
        window.$ = layui.$;
        var layer = layui.layer;
        /**
         * 备份表方法
         */
        $("#export").click(function(){
            $(this).html("正在发送备份请求...");
            $.post(
                $("#export-form").attr("action"),
                $("#export-form").serialize(),
                function(data){
                    if(data.code==1){
                        $("#export").html( "开始备份，请不要关闭本页面！");
                        backup(data.data.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    }else{
                        layer.tips(data.msg, "#export", {
                            tips: [1, '#3595CC'],
                            time: 4000
                        });
                        $("#export").html("立即备份");
                    }

                }, "json");
            return false;
        });


        /**
         * 查看表详细
         */
        $(".show").click(function () {
            var table=$(this).attr("data-table");
            CreateForm("查看表详情","70%","75%","/admin/databackup/show_table/table/"+table);
            return false;
        });

        /**
         * 递归备份表
         * @param tab
         * @param status
         */
        function backup(tab,status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get( $("#export-form").attr("action"), tab, function(data){
                // console.log(data)
                if(data.code==1){
                    showmsg(tab, data.msg);

                    if(!$.isPlainObject(data.data.tab)){
                        $("#export").html("备份完成");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }

                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    $("#export").html("立即备份");
                }
            }, "json");

        }


        /**
         * 修改备份状态
         * @param tab
         * @param msg
         */
        function showmsg(tab, msg){
            $("table tbody tr").eq(tab.id).find(".info").html(msg)
        }

        /**
         * 优化表
         */
        $("#optimize").click(function(){
            $.post(this.href, $("#export-form").serialize(), function(data){

                layer.tips(data.msg, "#optimize", {
                    tips: [1, '#3595CC'],
                    time: 4000
                });

            }, "json");
            return false;
        });


        /**
         * 修复表
         */
        $("#repair").on("click",function(e){

            $.post(this.href, $("#export-form").serialize(), function(data){
                layer.tips(data.msg, "#repair", {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
            }, "json");
            return false;
        });
    });

</script>


<script type="text/javascript">
    /**
     * 优化
     */
  $('.optimiz').click(function(){
      var tables = $(this).attr('tables');
      $.ajax({
        url:"{:url('admin/databackup/optimize')}"
        ,data:{tables:tables}
        ,success:function(res){
          layer.msg(res.msg);
        }
      })
  });

    /**
     * 修护
     */
  $('.repair').on('click',function(){
    var tables = $(this).attr('tables');
    $.ajax({
      url:"{:url('admin/databackup/repair')}"
      ,data:{tables:tables}
      ,success:function(res){
        layer.msg(res.msg);
      }
    })
  });

</script>

  </body>
</html>
